package com.aaron.kjviewutils.health;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.footballleague.TabPagerStateAdapter;
import com.aaron.kjviewutils.wechatsift.WeChatFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

/**
 * 作者：Aaron on 2017/2/20 16:36
 * 邮箱：kaijin.fang@edianniu.com
 */
public class HealthActivity extends BaseActivity {

    @Bind(R.id.healthToolBar)
    Toolbar healthToolBar;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.healthTabs)
    VerticalTabLayout healthTabs;
    @Bind(R.id.healthViewpager)
    ViewPager healthViewpager;

    private TabPagerStateAdapter adapter;
    private List<Fragment> fragments;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        closeSelf(HealthActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_health);
        ButterKnife.bind(this);
        initTitle();
        initContent();
    }

    private void initContent()
    {
        fragments = new ArrayList<Fragment>();
        String[] foots = resources.getStringArray(R.array.health_type);
        if(null!=foots&&foots.length>0)
        {
            for(String foot:foots)
            {
                HealthFragment healthFragment = new HealthFragment();
                Bundle bundle = new Bundle();
                bundle.putString("foot", foot);
                healthFragment.setArguments(bundle);
                fragments.add(healthFragment);
            }
            adapter = new TabPagerStateAdapter(getSupportFragmentManager(), fragments);
            healthViewpager.setAdapter(adapter);
            healthTabs.setupWithViewPager(healthViewpager);
            healthTabs.setTabMode(VerticalTabLayout.TAB_MODE_SCROLLABLE);
            int size = foots.length;
            for(int i=0; i<size; i++)
            {
                String foot = foots[i];
                //初始化tabs
                TabView tabView = healthTabs.getTabAt(i);
                tabView.setTitle(new TabView.TabTitle.Builder().setContent(foot).setTextColor(0xFFFFFFFF, 0xFFD6DBE1).setTextSize(px2dp(HealthActivity.this.getResources().getDimension(R.dimen.pickerview_title))).build());
            }
        }
    }

    private void initTitle()
    {
        healthToolBar.setTitle("");
        barTitle.setText("健康大全");
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        healthToolBar.setNavigationIcon(null);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(HealthActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
